<template>
  <div class="Home"> 
    <van-nav-bar title="菜单" >
      <template #right >
        <van-icon name="cart-o" :badge="count>0 ? count:'' " @click="viewBag" color="#1989fa" />
      </template>
    </van-nav-bar>

    <!-- 图片 -->
    <div class="pic">
      <img src="../assets/icon/lb.jpg" alt="" width="100%" height="100px" />
    </div>

    <div class="menuproductList">
      <div class="menuproductList-left">
        <div
          class="menuproductList-left-item"
          :class="{ change: activeIndex == index }"
          @click="toggle(index, item.type)"
          v-for="(item, index) in Menu"
          :key="index"
        >
          {{ item.typeDesc }}
        </div>
      </div>
      <div class="menuproductList-right">
        <div class="productList" v-for="(item, index) in proData" @click="viewDetail(item.pid,item.type)" :key="index">
          <div class="productListbox">
            <div class="productListimg">
              <img :src="item.smallImg" alt="" width="60px" height="60px" />
            </div>
            <div class="pdtext">
              <div class="pdName">
                <div class="productListname">{{ item.name }}</div>
                <div class="productListenname">{{ item.enname }}</div>
              </div>
              <div class="productListprice">￥{{ item.price }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Menu",
  data() {
    return {
      count:'',
      activeIndex: 0,
      Menu: [],
      proData: [],
    };
  },
  created() {
    this.getProductType();
    this.findShopbagCount()
  },
  methods: {
    //查询购物袋数量
    findShopbagCount(){
      let tokenString = this.$cookies.get("TOKEN")
      this.axios({
        method:"get",
        url:"/shopcartRows",
        params:{
          appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
          tokenString
        }
      }).then((res)=>{
        this.count = res.data.result
        console.log(res);
      })
    },
    //点击跳转购物袋
    viewBag(){
      this.$router.push({path:"/Bag"})
    },
    //点击跳转商品详情
    viewDetail(pid,type){
      this.$router.push({name:"Detail",query:{pid,type}})
    },
    back() {
      this.$router.go(-1);
    },
    //点击切换
    toggle(e, type) {
      if (this.activeIndex == e) {
        return;
      }
      this.activeIndex = e;
      console.log(type);
      this.getProductData(type);
    },
    //获取商品分类
    getProductType() {
      this.axios({
        method: "get",
        url: "/type",
        params: {
          appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
        },
      }).then((res) => {
        this.Menu = res.data.result;
        this.getProductData(res.data.result[this.activeIndex].type);
        console.log(this.Menu);
      });
    },
    //根据商品分类获取商品
    getProductData(type) {
      //如果有数据，就不需要发请求
      let catchData = localStorage.getItem("pro")
      catchData = catchData  ? JSON.parse(catchData) : {}
      if(catchData[type]){
        this.proData = catchData[type]
        return
      }
      this.axios({
        method: "get",
        url: "/typeProducts",
        params: {
          appkey: "U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=",
          key: "type",
          value: type,
        },
      }).then((res) => {
        this.proData = res.data.result;
        catchData[type] = res.data.result
        localStorage.setItem("pro",JSON.stringify(catchData))
        console.log(res);
      });
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.van-icon-cart-o:before{
  font-size: 18px;
}
.menuproductList {
  position: fixed;
  top: 154px;
  left: 0;
  right: 0;
  bottom: 40px;
  display: flex;
  .menuproductList-right {
    padding-bottom: 30px;
    overflow-y: scroll;
    flex: 1;
    flex-wrap: wrap;
    .productListbox {
      height: 80px;
      display: flex;
      background-color: #fff;
      margin: 10px;
      .productListimg {
        margin: 10px;
      }
      .pdtext {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .pdName {
          flex: 3;
          margin-top: 5px;
          font-size: 12px;
          .productListenname {
            margin-top: 5px;
            font-size: 10px;
          }
        }
        .productListprice {
          font-weight: bold;
          color: #001bb2;
          font-size: 16px;
          flex: 1;
          margin-left: auto;
        }
      }
    }
  }
  .menuproductList-left {
    width: 80px;
    .menuproductList-left-item {
      line-height: 40px;
      height: 40px;
      text-align: center;
    }
    .change {
      background-color: #fff;
      color: skyblue;
    }
  }
}
</style>